import { getItem, setItem, removeItem } from '@/utils/storage.js'

const USER_KEY = 'login_user'

const user = {
  namespaced: true,

  state: {
    user: getItem(USER_KEY)
  },

  mutations: {
    setUser (state, data) {
      const currentTimestamp = Date.parse(new Date())
      data.expire_at = currentTimestamp + 7200000 // token两个小时过期
      state.user = data
      setItem(USER_KEY, data)
    },
    deleteUser (state) {
      removeItem(USER_KEY)
      state.user = null
    },
    refreshToken (state, token) {
      state.user.token = token
      setItem(USER_KEY, state.user)
    }
  },

  actions: {
  }
}
export default user
